/**
 * 
 */
package com.olclass.web.support;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;
import org.codehaus.plexus.util.StringUtils;
import com.olclass.web.support.Progress;


public class FileUploadProgressListener implements ProgressListener{
    
	private HttpSession session;
	public final static String UploadStatus = "upload-progress";
	
	private String uploadTag = "upload-progress";
	
	
	public FileUploadProgressListener(HttpSession session) {
	    this.session=session;  
        Progress progress = new Progress();
        
        if(session.getAttribute("workId") != null){
        	this.uploadTag = session.getAttribute("workId").toString();
        }
        session.setAttribute(this.uploadTag, progress);  
    }  
	
	public FileUploadProgressListener(HttpSession session,String workId) {
	    this.session=session;  
        Progress progress = new Progress();
        if(StringUtils.isNotBlank(workId)){
        	this.uploadTag = workId;
        }
        session.setAttribute(this.uploadTag, progress);  
	 }  
		
		/**
		 * pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件
		 */
	public void update(long pBytesRead, long pContentLength, int pItems) {
			Progress status = (Progress) session.getAttribute(this.uploadTag);
			status.setBytesRead(pBytesRead);
			status.setContentLength(pContentLength);
			status.setItems(pItems);
			session.setAttribute(this.uploadTag, status);
			//System.out.println(pItems + ":" + pBytesRead);
	}

}
